﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com     Website: http://www.wishenterprise.com
 * 
 * */

using System.Text;
using Origami.Security;

namespace SmsRepository.Local
{
    /// <summary>
    /// User Security
    /// Password Encryptor/Decryptor
    /// </summary>
    public class UserCryptoService
    {
        /// <summary>
        /// Encrypts the specified s user id.
        /// </summary>
        /// <param name="usr">The s user id.</param>
        /// <param name="pwd">The plain password.</param>
        /// <returns></returns>
        public static string Encrypt(
            string usr,
            string pwd)
        {
            try
            {
                string phase1 =
                    GetSymmetricCrypthography(SymmetricProvider.Rijndael, usr).Encrypt(pwd);
                string phase2 =
                    GetSymmetricCrypthography(SymmetricProvider.TripleDES, phase1).Encrypt(phase1);
                return phase2;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// Decrypts the specified s user id.
        /// </summary>
        /// <param name="usr">The s user id.</param>
        /// <param name="pwd">The encrypted password.</param>
        /// <returns></returns>
        public static string Decrypt(string usr, string pwd)
        {
            try
            {
                string phase1 =
                    GetSymmetricCrypthography(SymmetricProvider.TripleDES, usr).Decrypt(pwd);
                string phase2 =
                    GetSymmetricCrypthography(SymmetricProvider.Rijndael, phase1).Decrypt(phase1);
                return phase2;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// Gets the symmetric crypthography.
        /// </summary>
        /// <param name="prov">The symmetric provider.</param>
        /// <param name="usr">The s user id.</param>
        /// <returns></returns>
        private static SymmetricCryptography GetSymmetricCrypthography(
            SymmetricProvider prov,
            string usr)
        {
            return new SymmetricCryptography(prov)
                       {
                           Key =
                               GenerateKey(usr),
                           Salt = "17j4n0e41r1t4h0en2o09"
                       };
        }

        /// <summary>
        /// Generates the key.
        /// </summary>
        /// <param name="usr">The s user id.</param>
        /// <returns></returns>
        private static string GenerateKey(string usr)
        {
            var sb = new StringBuilder();
            sb.Append(usr);
            sb.Append("f4o3z1yj4hz4h12a4zk1y4");
            return sb.ToString().Trim();
        }
    }
}